<?php 
/**
 * Action Helper for loading forms
 * 
 * @uses Zend_Controller_Action_Helper_Abstract
 */
class Source_Controller_Action_Helper_Loader extends Zend_Controller_Action_Helper_Abstract
{
    
	private static $_container = array();
	
	public function form($name,$options = null)
	{
		return $this->loadObject(__METHOD__, $name, $options);
	}
	
	public function model($name,$options = null)
	{
		return $this->loadObject(__METHOD__, $name, $options);
	}
	
	public function mapper($name,$options = null)
	{
		return $this->loadObject(__METHOD__, $name, $options);
	}
	
	public function table($name,$options = null)
	{
		return $this->loadObject(__METHOD__, $name, $options);
	}
	
	public function plugin($name,$options = null)
	{
		return $this->loadObject(__METHOD__, $name, $options);
	}
	
	public function service($name,$options = null)
	{
		return $this->loadObject(__METHOD__, $name, $options);
	}
	
    /**
     * Load a form with the provided options
     * 
     * @param  string $name 
     * @param  array|Zend_Config $options 
     * @return Zend_Form
     */
    public function loadObject($type, $name, $options = null)
    {            	
    	$type = str_replace('Source_Controller_Action_Helper_Loader::',null,$type);
    	
    	if(stristr($name, '/')){
        	$parts = explode('/',$name);
        	$module = $parts[0];
        	$class  = $this->_fixName($parts[1]);        	
        } else {
        	$module  = $this->getRequest()->getModuleName();
        	$class   = $this->_fixName($name);	
        }
                
        switch($type){
        	case 'form':
        		$prefix = ucfirst($module) . '_Form_';
        		break;
        	case 'model':
        		$prefix = ucfirst($module) . '_Model_';
        		break;
        	case 'plugin':
        		$prefix = ucfirst($module) . '_Plugin_';
        		break;
        	case 'service':
        		$prefix = ucfirst($module) . '_Service_';
        		break;
        	case 'mapper':
        		$prefix = ucfirst($module) . '_Model_Mapper_';
        		break;
        	case 'table':
        		$prefix = ucfirst($module) . '_Model_DbTable_';
        		break;			
        }

        $class = $prefix . $class;
        
        if(class_exists($class)){
        	$class = new $class($options);
        	return $class;
        }
        
        throw new Source_Exception("$class does not exist.");
    }
    
    /**
     * Strategy pattern: call helper as broker method
     * 
     * @param  string $name 
     * @param  array|Zend_Config $options 
     * @return Zend_Form
     */
    public function direct($type, $name, $options = null)
    {
        return $this->loadObject($type, $name, $options);
    }
    
    /**
     * Fix name
     * 
     * @param unknown_type $spec
     */
    private function _fixName($spec)
    {
    	$parts = explode('_', $spec);
    	$temp  = array();
    	foreach($parts as $part){
    		$temp[] = ucfirst($part);
    	}
    	return implode('_',$temp);
    }
}
